package com.kk.xhr.admin.controller;

import com.kk.xhr.common.RedisConstant;
import com.kk.xhr.model.entity.Nation;
import com.kk.xhr.model.service.INationService;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 民族管理控制器
 *
 * @author kkmystery
 * @version 1.0 2021/3/5
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "民族管理", tags = "民族管理")
@RequestMapping("/nation")
public class NationController {
    private final Executor adminControllerBizExecutor;
    private final INationService nationService;
    private final RedisTemplate<String, Object> redisTemplate;

    @ApiOperation(value = "查询民族")
    @GetMapping
    public CompletionStage<ApiResult<List<Nation>>> query() {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(getNationCache()), adminControllerBizExecutor);
    }

    private List<Nation> getNationCache() {
        List<Nation> list = (List<Nation>) redisTemplate.opsForValue().get(RedisConstant.NATION_CACHE + ":" + "list");
        if (null == list || list.isEmpty()) {
            list =nationService.query();
            redisTemplate.opsForValue().set(RedisConstant.NATION_CACHE + ":" + "list", list);
        }
        return list;
    }
}
